package com.songshuai.pangu.api.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.songshuai.pangu.api.component.ServiceItemComponent;
import com.songshuai.pangu.api.dto.pangu.GetInviteServiceItemTreeResDto;
import com.songshuai.pangu.api.service.ServiceItemService;
import com.songshuai.webapi.baseapi.dto.pangu.request.service.AddInviteServiceItemReqDto;
import com.songshuai.webapi.baseapi.dto.pangu.request.service.ServiceReqDto;
import com.songshuai.webapi.baseapi.dto.pangu.response.service.InviteServiceItemTreeResDto;
import com.songshuai.webapi.baseapi.enumerations.ResultStatus;
import com.songshuai.webapi.baseapi.exception.BusinessException;
import com.songshuai.webapi.baseapi.pojo.QueryRes;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: lhx
 * @Date: 2019/4/11 10:18
 */
@Slf4j
@Service
public class ServiceItemServiceImpl implements ServiceItemService {



    @Autowired
    private ServiceItemComponent serviceItemComponent;


    /**
     * 获取所有服务标准化集合
     * @return
     */
    public List<InviteServiceItemTreeResDto> getAllServiceList() {
        String itemTree = serviceItemComponent.getInviteServiceItemTree();
        log.info("获取所有服务标准化集合-盘古返回参数：[{}]",itemTree);
        if (StringUtils.isNotBlank(itemTree)){
            QueryRes<List<GetInviteServiceItemTreeResDto>> queryRes = JSON.parseObject(itemTree, new TypeReference<QueryRes<List<GetInviteServiceItemTreeResDto>>>() {});
            if (ResultStatus.SUCCESS.getErrorCode() == queryRes.getStatus()){
                if(null != queryRes.getResult()){
                    List<GetInviteServiceItemTreeResDto> result = queryRes.getResult();
                    return result.stream().map(x -> {
                        return new InviteServiceItemTreeResDto(){{
                            setId(x.getId());
                            setName(x.getName());
                            setContent(x.getContent());
                            setNodeId(x.getNodeId());
                            setParentId(x.getParentId());
                            setSort(x.getSort());
                            setChildrenList(getChildList(x.getChildrenList()));
                        }};
                    }).collect(Collectors.toList());
                }
                return new ArrayList<>();
            }else {
                throw new BusinessException(queryRes.getInfo(),ResultStatus.FAILOfficial);
            }
        }

        return null;
    }

    /**
     * 递归获取子集
     * @param childrenList
     * @return
     */
    private List<InviteServiceItemTreeResDto> getChildList(List<GetInviteServiceItemTreeResDto> childrenList){
        if (!CollectionUtils.isEmpty(childrenList)){
           return childrenList.stream().map(x -> {
                return new InviteServiceItemTreeResDto(){{
                    setId(x.getId());
                    setName(x.getName());
                    setContent(x.getContent());
                    setNodeId(x.getNodeId());
                    setParentId(x.getParentId());
                    setSort(x.getSort());
                    setChildrenList(getChildList(x.getChildrenList()));
                }};
            }).collect(Collectors.toList());
        }
        return new ArrayList<>();
    }


    /**
     * 获取邀约服务已反馈信息
     * @param reqDto
     * @return
     */
    public List<String> getInviteServiceItems(ServiceReqDto reqDto) {
        log.info("获取邀约服务已反馈信息-请求参数：[{}]",JSON.toJSONString(reqDto));
        String inviteServiceItems = serviceItemComponent.getInviteServiceItems(reqDto);
        log.info("获取邀约服务已反馈信息-盘古返回参数：[{}]",inviteServiceItems);
        if(StringUtils.isNotBlank(inviteServiceItems)){
            QueryRes<List<String>> queryRes = JSON.parseObject(inviteServiceItems, new TypeReference<QueryRes<List<String>>>() {});
            if (ResultStatus.SUCCESS.getErrorCode() == queryRes.getStatus()){
                if(null != queryRes.getResult()){
                    return queryRes.getResult();
                }
                return new ArrayList<>();
            }else {
                throw new BusinessException(queryRes.getInfo(),ResultStatus.FAILOfficial);
            }
        }
        return null;
    }




    /**
     * 新增邀约服务反馈信息
     * @param reqDto
     * @return
     */
    public Boolean addInviteServiceItem(AddInviteServiceItemReqDto reqDto) {
        log.info("新增邀约服务反馈信息-请求参数：[{}]",JSON.toJSONString(reqDto));
        String serviceItem = serviceItemComponent.addInviteServiceItem(reqDto);
        log.info("新增邀约服务反馈信息-盘古返回参数：[{}]",serviceItem);
        if(StringUtils.isNotBlank(serviceItem)){
            QueryRes<Boolean> response = JSON.parseObject(serviceItem, new TypeReference<QueryRes<Boolean>>() {});
            if (ResultStatus.SUCCESS.getErrorCode() == response.getStatus()){
                return response.getResult();
            }else {
                throw new BusinessException(response.getInfo(),ResultStatus.FAILOfficial);
            }
        }
        return false;
    }


}
